﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;

namespace EFContrib.PS4EF.Tst
{

    public static class DataContractSerializerExtensions
    {
        public static string GetWellFormedToContract(this DataContractSerializer s, object graph)
        {
            MemoryStream m = new MemoryStream();
            XmlTextWriter tw = new XmlTextWriter(m, Encoding.UTF8);
            tw.Formatting = Formatting.Indented;
            tw.Indentation = 4;
            tw.IndentChar = ' ';

            s.WriteObject(tw, graph);
            tw.Flush();

            m.Position = 0;
            StreamReader sr = new StreamReader(m);
            string strOutput = sr.ReadToEnd();

            return strOutput;
        }

        public static object UnwrapFromString(this DataContractSerializer s, string message)
        {
            StringReader reader = new StringReader(message);
            XmlReader xreader = XmlReader.Create(reader);
            return s.ReadObject(xreader);
        }
    }
}
